/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.devspan.tokbox;

import com.devspan.tokbox.domain.TokBoxAccount;
import java.io.IOException;
import java.sql.Connection;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Harlan
 */
public class TokBoxServlet extends HttpServlet {

    Connection conn;
    TokBox tokBox;
    String passwordSalt;

    @Override
    public void destroy() {
        super.destroy();
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        try {
            super.init(config);      
            
            Properties props = new Properties();
            props.load(config.getServletContext().getResourceAsStream("/WEB-INF/classes/tokbox.properties"));
            
            passwordSalt = props.getProperty("passwordSalt", "I'm an idiot and will have security problems.");
            
            tokBox = new TokBox(props.getProperty("key"), props.getProperty("secret"), props.getProperty("url", TokBox.TOKBOX_API_URL));

        } catch (Exception e) {
            throw new ServletException(e);
        }
    }

    @Override    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String op = request.getParameter("op");
            if( "getInfo".equals(op) ) {
                String email = request.getParameter("email");
                
                // encode the password with salt, so their actual password
                // will be an md5 hash.
                String password = tokBox.getMD5(passwordSalt + email);
                
                String userId = null;
                String widget = null;
                boolean isNew = false;
                
                try {
                    userId = tokBox.getUserId(email, password);
                    widget = tokBox.getWidget(email, password);
                } catch(TokBox.TokBoxInternalException e ) {
                    throw e;
                } catch(TokBox.TokBoxException e ) {
                    // register the user if their userId doesn't exist.
                    
                    String firstName = email.split("@")[0];
                    
                    TokBoxAccount a = tokBox.registerUser(email, password, firstName, "Soashable", null);
                    userId = a.getUserId();
                    widget = a.getWidget();
                    
                    isNew = true;
                }
 
                response.getWriter().println("{\"success\":true,\"user_id\":\""+userId+"\",\"password\": \""+password+"\", \"widget_id\":\""+widget+"\", \"new\": "+String.valueOf(isNew)+"}");
            } else {
                response.getWriter().println("{\"success\":false,\"error_code\":1,\"message\":\"Invalid command.\"}");
            }
            
        } catch (Exception e) {
            throw new ServletException(e);
        }
    }
}
